#!/bin/sh

# $Id$

file_list=".cvsignore Makefile.in Makefile.frag config.m4 php_skeleton.c php_skeleton.h skeleton.overrides"

giveup() {
	echo $*
	echo ""
	exit 1
}

usage() {
	echo "$0 --widget=name"
	echo ""
	echo " --widget=name  the name of the widget."
	echo "                It's actually important to name the"
	echo "                widget properly, e.g. SQPaned and"
	echo "                not Sqpaned."
	echo ""
	exit 1
}

if test $# = 0; then
	usage
fi

while test $# -gt 0; do
	case "$1" in
	-*=*) optarg=`echo "$1" | sed 's/[-_a-zA-Z0-9]*=//'` ;;
	*) optarg= ;;
	esac

	case $1 in
		--widget=?*)
		Widget=$optarg
		WIDGET=`echo $Widget | tr [a-z] [A-Z]`
		widget=`echo $Widget | tr [A-Z] [a-z]`
		;;
		*)
		usage
		;;
	esac
	shift
done

if test -d "$widget"; then
	giveup "Directory $widget already exists."
fi

test -f ext_skel || giveup "ext_skel must be in the current directory"
test -d skeleton || giveup "subdirectory skeleton does not exist or is not directory"

echo "Creating directory $widget"
mkdir $widget || giveup "Cannot create directory $widget"

echo "Creating skeleton files"
cd skeleton
cp -a $file_list ../$widget/
cd ../$widget/

perl -p -i -e "s/skeleton/$widget/g" $file_list
perl -p -i -e "s/Skeleton/$Widget/g" $file_list
perl -p -i -e "s/SKELETON/$WIDGET/g" $file_list
perl -p -i -e "s/^dnl  //" config.m4

mv php_skeleton.c php_$widget.c
mv php_skeleton.h php_$widget.h
mv skeleton.overrides $widget.overrides

echo "Skeleton built."
echo ""
echo "Now you can create your $widget/$widget.defs file with:"
echo ""
echo  -e "\t../generator/h2def.py <path>/$widget.h >$widget/$widget.defs"
echo ""
echo "and don't forget to add the constructor line to $widget.defs:"
echo ""
echo -e "\t(is-constructor-of Gtk$Widget)"
echo ""
echo "in the definition of gtk_${widget}_new"
echo ""
